require "base_def"

local hand_agent_info = {}

function hand_agent_info:Init()
    --当前连接中的agent表
    hand_agent_info.agent = {}
    --当前处于掉线的agent表
    hand_agent_info.discntAgent = {}
end

--这里的登录成功是指重新登录[从DB取的数据而不是重连的数据恢复]
--客户端成功登录后agent的信息更新(fd一定会发生变化)
function hand_agent_info:AgentLoginOk(args)
    log.info("hand_agent_info:AgentLoginOk args=", args)
    --error
    if not args.channel then 
        log.error("hand_agent_info:AgentLoginOk error args.channel=", args)
        return
    end
    if not args.gold then 
        log.error("hand_agent_info:AgentLoginOk error args.gold=", args)
        return
    end
    if not args.headimgurl then 
        log.error("hand_agent_info:AgentLoginOk error args.headimgurl=", args)
        return
    end
    if not args.nickname then 
        log.error("hand_agent_info:AgentLoginOk error args.nickname=", args)
        return
    end
    if not args.safebox_coin then 
        log.error("hand_agent_info:AgentLoginOk error args.safebox_coin=", args)
        return
    end
    if not args.userid then 
        log.error("hand_agent_info:AgentLoginOk error args.userid=", args)
        return
    end
    if not args.vip_level then 
        log.error("hand_agent_info:AgentLoginOk error args.vip_level=", args)
        return
    end

    local ok, res = skynet.call(".hand_socket_connect", 'lua', "GetAgentConnectInfo", args.fd)
    if ok then 
        --这里的登录成功是指重新登录，agent和discntAgent表里都不应该有数据
        if self.agent[args.userid] then 
            log.error("hand_agent_info:AgentLoginOk self.agent already have agent userid= ", args.userid)
        end
        if self.discntAgent[args.userid] then 
            log.error("hand_agent_info:AgentLoginOk self.discntAgent already have agent userid= ", args.userid)
        end
        local agent = {}
        agent.fd = res.fd
        agent.addr = res.addr
        agent.connType = res.connType
        --
        agent.channel = args.channel
        agent.gold = args.gold
        agent.headimgurl = args.headimgurl
        agent.nickname = args.nickname
        agent.safebox_coin = args.safebox_coin
        agent.userid = args.userid
        agent.vip_level = args.vip_level
        self.agent[args.userid] = agent
        log.info("self.agent=", self.agent)
        return true
    else
        log.error("hand_agent_info:AgentLoginOk error res=", res)
    end
end

--登录逻辑：
--1：缓存token登录：

--游客登录逻辑：


--首先gate监听到socket的open,此时产生一个新的fd。sn服务保存这个agent的基本信息
--当agent进入登录逻辑时，服务器检测本次登录的
--as通过查表知道此次是

--客户端掉线
function hand_agent_info:AgentDisconnect(userId)
    local agent = TableCopy(self.agent[userId])
    if agent then
        agent.last_beat_time = os.time()
        self.discntAgent[userId] = agent
        self.agent[userId] = nil
        log.info("AgentDisconnect agent=", agent)
    else 
        log.error("hand_agent_info:AgentDisconnect error userId=", userId)
    end
end

--断线重连:重连的玩家数据从进程内存恢复，重新登录的玩家从DB内取数据
--因为一些临时数据：如是否在桌子内，当前操作数据是不存入db的而是在内存中
function hand_agent_info:AgentReconnect(userId)
    local agent = TableCopy(self.discntAgent[userId])
    if agent then 
        agent.last_beat_time = nil
        self.agent[userId] = agent
        self.discntAgent[userId] = nil
        log.info("AgentReconnect agent=", agent)
    end
end

--修改agent的数据
function hand_agent_info:ModifyAgent(userId, args)
    local agent = self.agent[userId]
    if agent then 
        for k, v in pairs(args) do
            agent[k] = v
        end
    else
        log.error("ModifyAgent error no agent!", userId)
    end
end

--询问agent是否加入过房间,大厅重连时调用
function hand_agent_info:CheckJoinGameInfo(args)  
    local info = PlayersInRoomList[args.player_id]
    log.info('ask_joined_gameinfo, info:', info)
    --这几个游戏掉线时如果没在玩 则不会回游戏界面
    if info and (info.game_type == g_gameType.WZRY or    
       info.game_type == g_gameType.SGJ) then
        log.info('CheckJoinGameInfo, wzry or sgj, info:', info)
        if not info.srv_id then
            return
        end
    end

    if info and info.game_type then
        local game_reg_info = skynet.call('.game_reg', 'lua', 'get_game_info')
        local one_game = game_reg_info[info.game_type]
        if not next(one_game or {}) then
            log.warning('player in game, but game process not exist!', args.player_id, info.game_type)
            return
        end
    end

    if info and info.friend_room_id then  --如果是俱乐部房
        local friend_game = FriendRoomIdList[info.friend_room_id]
        info.club_id = friend_game and friend_game.club_info and friend_game.club_info.club_id
    end

    return info
end

function hand_agent_info:GetUserInfo(userid)
    if self.agent[userid] then 
        return true, self.agent[userid]
    end
    log.error("GetUserInfo error self.agent=", self.agent)
end

skynet.start(function()
    skynet.dispatch("lua", function(session, address, cmd, ...)
        --log.info("redis_tools dispatch cmd=", cmd)
        local f = hand_agent_info[cmd]
        if f then
            skynet.ret(skynet.pack(f(hand_agent_info, ...)))
        else
            log.error(string.format("Unknown command %s", tostring(cmd)))
        end
    end)

    skynet.register(".agent_status")
    hand_agent_info:Init()
end)

return hand_agent_info